<?php

namespace app\common\model;

use think\Model;

class Mineral extends Model
{
    // 表名
    protected $name = 'mineral';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = false;
    
    // 追加属性
    protected $append = [
        'expiretime_text',
        'status_text',
        'expire'
    ];
    
    public function getExpireAttr($value, $data)
    {
        if ($data['yield']*0.05 > $data['yield']-$data['total']) {
            return 1;
        }else{
            return 0;
        }
        
    }
    
    public function getStatusList()
    {
        return ['running' => __('Status running'),'scrapped' => __('Status scrapped')];
    }     


    public function getExpiretimeTextAttr($value, $data)
    {
        $value = $value ? $value : $data['expiretime'];
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getStatusTextAttr($value, $data)
    {        
        $value = $value ? $value : $data['status'];
        $list = $this->getStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    protected function setExpiretimeAttr($value)
    {
        return $value && !is_numeric($value) ? strtotime($value) : $value;
    }


    public function user()
    {
        return $this->belongsTo('User', 'user_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }
}
